package com.obor.quartz.job.biz;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import org.springframework.http.HttpMethod;
import org.zn.mvcbase.rest.EntityBody;
import org.zn.mvcbase.rest.RestEntity;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.base.Strings;
import com.obor.quartz.job.JobInfo;
import com.obor.quartz.job.QuartzJob;

/**
 * 
 * @Company OBOR
 * @Author 周楠
 * @Description 2019年4月10日
 *
 */
public class MsApiJob extends QuartzJob {


	

	@Override
	public void doJob() throws JsonProcessingException, IOException {
		// TODO Auto-generated method stub

		JobInfo ji = (JobInfo) dataMap.get("info");

		String url = ji.getApiUrl();
		String ms = ji.getMsName();
		String method = ji.getMethod();
		Map<String, Object> paras = ji.getParameters();

		RestEntity re = ref.getRestEntity(transformMethod(method), "http://"+ms + url);
				
		if(Objects.nonNull(paras)) {
			re.addVariables(paras);
		}
		
		EntityBody eb = re.sendRequest();

		Map<Object, Object> result = eb.toJavaObject(new TypeReference<Map<Object, Object>>() {
		});

		System.out.println(result);

	}

	private HttpMethod transformMethod(String method) {

		HttpMethod hm = HttpMethod.GET;

		if(Strings.isNullOrEmpty(method)) {
			
			return  HttpMethod.GET;
		}
		
		if (HttpMethod.GET.name().equals(method.toUpperCase())) {
			hm = HttpMethod.GET;
		} else if (HttpMethod.POST.name().equals(method.toUpperCase())) {

			hm = HttpMethod.POST;

		} else if (HttpMethod.DELETE.name().equals(method.toUpperCase())) {

			hm = HttpMethod.DELETE;

		} else if (HttpMethod.OPTIONS.name().equals(method.toUpperCase())) {

			hm = HttpMethod.OPTIONS;

		} else if (HttpMethod.HEAD.name().equals(method.toUpperCase())) {

			hm = HttpMethod.HEAD;

		} else if (HttpMethod.PATCH.name().equals(method.toUpperCase())) {

			hm = HttpMethod.PATCH;

		} else if (HttpMethod.PUT.name().equals(method.toUpperCase())) {

			hm = HttpMethod.PUT;

		} else if (HttpMethod.TRACE.name().equals(method.toUpperCase())) {

			hm = HttpMethod.TRACE;

		}

		return hm;

	}

}
